#include "materials.h"
#include "shaders.h"
#include "textures.h"
#include "colors.h"

#include <cstdlib>
#include <fstream>
#include <iostream>
using namespace std;

void CMaterial::PassToShader(ShaderProgram *pShader)
{
    for(unsigned int i = 0; i < Textures.size(); ++i)
    {
        char chName[] = "Texture\0\0\0";
        char chEnd[4] = "\0\0\0";
        itoa(i, chEnd, 10);

        chName[7] = chEnd[0];
        chName[8] = chEnd[1];
        chName[9] = chEnd[2];

        pShader->SetTexture(chName, Textures[i]->GetId(), i);
    }
    for(unsigned int i = 0; i < Colors.size(); ++i)
    {
        char chName[] = "Color\0\0\0\0";
        char chEnd[4] = "\0\0\0";
        itoa(i, chEnd, 10);

        chName[5] = chEnd[0];
        chName[6] = chEnd[1];
        chName[7] = chEnd[2];

        float col[3] = {Colors[i]->r, Colors[i]->g, Colors[i]->b};

        pShader->SetVector3(chName, col);
    }
}

void CMaterial::ChangeColor(int nWhich, Color3 *pNew)
{
    Colors[nWhich] = pNew;
}

void CMaterial::ChangeTexture(int nWhich, Texture *pNew)
{
    Textures[nWhich] = pNew;
}

int CMaterial::AddColor(Color3 *pNewColor)
{
    Colors.push_back(pNewColor);

    return Colors.size()-1;
}

int CMaterial::AddTexture(Texture *pNewTex)
{
    Textures.push_back(pNewTex);

    return Textures.size()-1;
}

Color3* CMaterial::GetColorPointer(int nWhich)
{
    return Colors[nWhich];
}

Texture* CMaterial::GetTexturePointer(int nWhich)
{
    return Textures[nWhich];
}

void CMaterial::SetTexturesNo(int nNo)
{
    Textures.reserve(nNo);
}

void CMaterial::SetColorsNo(int nNo)
{
    Colors.reserve(nNo);
}
